<template>
  <div class="table_content">
    <el-card>
      <div style="text-align: right; margin-bottom: 20px;display: flex;justify-content: right;">
        <div style="width:200px;margin-right:10px;">
          <el-input v-model="userNameLike" placeholder="请输入内容" />
        </div>
        <el-button type="primary" size="small" @click="getRoleList('1')">查询</el-button>
        <el-button type="primary" size="small" @click="getRoleList('0')">重置</el-button>
        <el-button type="primary" size="small" @click="addMenu">添加用户</el-button>
      </div>
      <div class="table">
        <el-table
          :data="userInformation.records"
          size="small"
          :height="tbHeight"
          :header-cell-style="{background:'#FAFAFA'}"
          :row-style="{height:'58px'}"
        >
          <el-table-column prop="userName" label="姓名" align="center" />
          <el-table-column prop="mobile" label="手机号" align="center" />
          <el-table-column prop="certSn" label="certSn" align="center" />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <span class="msg77" @click="distribute(scope.row)">分配角色</span>
              <span class="msg99" @click="editCarbon(scope.row)">编辑</span>
              <span class="msg88" @click="delCarbon(scope.row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            :current-page="pageIndex"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
    <!-- 弹框 -->
    <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
      <el-form ref="formName" v-loading="loading" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="userName">
          <el-input v-model="formData.userName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="certSn">
          <el-input v-model="formData.certSn" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="clickSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 分配权限给用户的弹框 -->
    <el-dialog title="分配权限" :visible="showAssignDialog" @open="openAssignDialog" @close="closeAssginDialog">
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in roleArr" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

      </el-checkbox-group>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="closeAssginDialog">取消</el-button>
          <el-button type="primary" @click="clickSubmitAssign">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>

// 手机格式
export default {
  name: 'Menu3',
  data() {
    return {
      userData: [],
      tbHeight: 540,
      showTitle: '',
      showDialog: false,
      loading: false,
      formData: {
        userName: '',
        mobile: '',
        certSn: ''
      },
      pageIndex: 1, // 当前多少页
      pageSizes: [10, 30, 50, 100], // 显示多少条
      total: 0, // 总条数
      pageSize: 10, // 每页多少条
      Update: false, // 判断发送编辑接口/新增接口
      showAssignDialog: false, // 控制弹框的显示隐藏
      treeLoading: false, // 权限弹框
      assignList: [], // 存放所有的权限列表数据
      userNameLike: '',
      userInformation: {},
      checkList: [],
      roleArr: [],
      userInfo: {},
      rules: {
        userName: [
          { required: true, message: '请输入用户名称', trigger: ['blur'] },
          { max: 100, message: '用户名称长度最大100', trigger: ['blur'] }
        ],
        mobile: [
          { required: true, message: '请输入用户类型', trigger: ['blur'] },
          { min: 11, max: 11, message: '请输入正确的手机号', trigger: ['blur'] }
          // { validator: validatePass, trigger: ['blur'] }
        ],
        certSn: [
          { required: true, message: '请输入用户certSn', trigger: ['blur'] }
        ]

      }

    }
  },
  created() {
    const that = this
    // let ctHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
    // that.tbHeight = ctHeight - 310
    that.rbacUserQueryUserByNameLikel()
  },
  methods: {
    // 分配按钮
    distribute(row) {
      const that = this
      that.treeLoading = true
      that.showAssignDialog = true
      that.userInfo = row
    },
    // 点击确认, 进行提交分配
    async clickSubmitAssign() {
      const that = this
      that.rbacUserAddUserRole()
      that.showAssignDialog = false
    },
    // 权限弹框取消
    closeAssginDialog() {
      const that = this
      that.showAssignDialog = false
      that.roleArr = []
      // this.$refs.tree.setCheckedKeys([]) // 关闭时, 重置选中状态
    },
    // 弹框显示时, 触发的函数, 可以发送请求, 获取树形结构的数据
    async openAssignDialog() {
      const that = this
      this.treeLoading = true
      that.rbacRoleQuery()
      that.rbacUserQueryRole(that.userInfo.id)
      // 发送请求, 获取树形结构的列表
      this.treeLoading = false
    },
    // 获取全部角色
    rbacRoleQuery() {
      const that = this
      const data = {}
      that.$store.dispatch('role/rbacRoleQuery', data).then(res => {
        // console.log(res, '全部角色')
        if (res.data) {
          that.roleArr = res.data
        }
      })
    },
    // 查询按钮
    getRoleList(type) {
      const that = this
      if (type === '1') {
        that.rbacUserQueryUserByNameLikel()
      } else {
        that.userNameLike = ''
        that.rbacUserQueryUserByNameLikel()
      }
    },
    // 添加用户按钮
    addMenu() {
      const that = this
      that.showDialog = true
      that.showTitle = '新增用户'
    },
    // 删除按钮
    delCarbon(row) {
      const that = this
      that.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.rbacUserDelete(row.id)
      }).catch(() => { })
    },
    // 编辑按钮
    editCarbon(row) {
      const that = this
      // console.log(row, '编辑按钮')
      that.showTitle = '编辑用户'
      that.showDialog = true
      that.Update = true
      that.formData = {
        ...row
      }
    },
    // 删除菜单 接口
    rbacUserDelete(id) {
      const that = this
      const data = {
        id
      }
      that.$store.dispatch('userController/rbacUserDelete', data).then(res => {
        console.log(res, '删除用户')
        if (res.data) {
          that.rbacUserQueryUserByNameLikel() // 获取用户
          this.$message.success('删除成功')
        }
      })
    },
    // 更新用户(编辑)
    rbacUserUpdate() {
      const that = this
      const data = that.formData
      that.$store.dispatch('userController/rbacUserUpdate', data).then(res => {
        // console.log(res, '编辑')
        if (res.data) {
          that.showDialog = false
          that.rbacUserQueryUserByNameLikel() // 获取用户
          this.$message.success('编辑成功')
        }
      })
    },
    // 查询全部用户
    rbacUserQueryUserByNameLikel() {
      const that = this
      const data = {
        pageNo: that.pageIndex,
        pageSize: that.pageSize,
        userNameLike: that.userNameLike
      }
      that.$store.dispatch('userController/rbacUserQueryUserByNameLikel', data).then(res => {
        // console.log(res, '查询全部用户')
        that.userInformation = res.data
        that.total = res.data.total
        that.pageIndex = res.data.pages
        that.pageSize = res.data.size
      })
    },
    // 新增接口
    rbacUserAdd() {
      const that = this
      const data = that.formData
      that.$store.dispatch('userController/rbacUserAdd', data).then(res => {
        // console.log(res, '新增菜单')
        if (res.data) {
          that.showDialog = false
          that.$message.success('新增成功')
          that.formData = {
            userName: '',
            mobile: '',
            certSn: ''
          }
          that.rbacUserQueryUserByNameLikel() // 获取所有用户
        }
      })
    },
    // 绑定用户(id)和角色(id)之间的关系【包括新增更新】
    rbacUserAddUserRole() {
      const that = this
      const roleListArr = []
      this.checkList.forEach(item => {
        roleListArr.push({
          id: item
        })
      })
      const data = {
        user: {
          id: that.userInfo.id
        },
        roleList: roleListArr
      }

      that.$store.dispatch('userController/rbacUserAddUserRole', data).then(res => {
        console.log(res, '绑定用户(id)和角色(id)之间的关系【包括新增更新】')
        if (res.data) {
          that.$message.success('操作成功')
        }
      })
    },
    // 根据用户(id)查询关联菜单
    rbacUserQueryRole(id) {
      const that = this
      const data = {
        id
      }
      that.$store.dispatch('userController/rbacUserQueryRole', data).then(res => {
        // console.log(res, '根据用户(id)查询关联菜单')
        res.data.forEach(item => {
          that.checkList.push(item.id)
        })
      })
    },
    // 弹框确认
    clickSubmit() {
      const that = this
      this.$refs.formName.validate((valid) => {
        if (valid) {
          if (that.Update) {
            that.rbacUserUpdate() // 编辑
          } else {
            that.rbacUserAdd() // 新增
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 弹框取消
    closeDialog() {
      const that = this
      that.showDialog = false
      that.Update = false
      that.formData = {
        userName: '',
        mobile: '',
        certSn: ''
      }
    },
    // 每页多少条
    handleSizeChange(val) {
      this.pageSize = val
      this.searchReport()
    },
    // 当前页
    handleCurrentChange(val) {
      this.pageIndex = val
      this.searchReport()
    }

  }

}
</script>

<style lang="scss" scoped>
::v-deep .el-card__body {
  padding: 20px;
}
::v-deep .el-dialog__header_xqh {
  padding: 20px 20px 10px;
  background: #66b1ff;
  .el-dialog__title {
    color: #fff;
  }
}
.table_content {
  margin-top: 20px;
  width: 100%;
  background-color: #fff;
  border-radius: 4px;
  padding: 0 20px 20px 20px;
}
.table {
  overflow: auto;
  height: vhPt(1600);
  box-sizing: border-box;
  .empty {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    .img_w_h {
      width: 56px;
      height: 56px;
    }
  }
  .pagination {
    display: flex;
    justify-content: right;
    margin-top: 30px;
  }
}
</style>
